package com.cisdi.analysis.service.impl;

import com.cisdi.analysis.common.page.Page;
import com.cisdi.analysis.core.vo.ResultVO;
import com.cisdi.analysis.repository.mapper.AnalysisDataMapper;
import com.cisdi.analysis.service.DataAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liujunnan
 * @date 2018/5/24
 */
@Component
public class DataAnalysisServiceImpl implements DataAnalysisService {

    @Autowired
    private AnalysisDataMapper analysisDataMapper;

    @Override
    public ResultVO average() {
        //求得平均值
        ResultVO resultVO = analysisDataMapper.average();
        return resultVO;
    }

    @Override
    public ResultVO standardDeviation() {
        //求方差
        ResultVO resultVO = analysisDataMapper.standardDeviation();
        return resultVO;
    }

    @Override
    public Map<Object, Integer> outlier() {
        Map<Object, Integer> map = new HashMap(16);
        //存放平均值-3倍方差
        ResultVO resultMin = new ResultVO();
        //存放平均值+3倍方差
        ResultVO resultMax = new ResultVO();
        //求平均值
        ResultVO average = analysisDataMapper.average();
        //求方差
        ResultVO stand = analysisDataMapper.standardDeviation();
        //利用反射对对象进行相加求和放到resultMin和resultMax里面作为范围
        Field[] fields = ResultVO.class.getDeclaredFields();
        for (Field field : fields) {
            try {
                if (Double.class == field.getType()) {
                    String ge = field.getName();
                    StringBuilder name = new StringBuilder(ge.substring(0, 1).toUpperCase()).append(ge.substring(1));
                    Method g = ResultVO.class.getMethod("get" + name.toString());
                    Double max = (Double) g.invoke(average) + 3 * (Double) g.invoke(stand);
                    Double min = (Double) g.invoke(average) - 3 * (Double) g.invoke(stand);
                    Method s = ResultVO.class.getMethod("set" + name.toString(), Double.class);
                    s.invoke(resultMin, min);
                    s.invoke(resultMax, max);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        //求得离群值，放入map中
        int one = analysisDataMapper.selectOutlier("one", resultMin.getOne(), resultMax.getOne());
        int two = analysisDataMapper.selectOutlier("two", resultMin.getTwo(), resultMax.getTwo());
        int three = analysisDataMapper.selectOutlier("three", resultMin.getThree(), resultMax.getThree());
        int four = analysisDataMapper.selectOutlier("four", resultMin.getFour(), resultMax.getFour());
        int five = analysisDataMapper.selectOutlier("five", resultMin.getFive(), resultMax.getFive());
        int six = analysisDataMapper.selectOutlier("six", resultMin.getSix(), resultMax.getSix());
        int seven = analysisDataMapper.selectOutlier("seven", resultMin.getSeven(), resultMax.getSeven());
        int eight = analysisDataMapper.selectOutlier("eight", resultMin.getEight(), resultMax.getEight());
        int nine = analysisDataMapper.selectOutlier("nine", resultMin.getNine(), resultMax.getNine());
        int ten = analysisDataMapper.selectOutlier("ten", resultMin.getTen(), resultMax.getTen());
        int eleven = analysisDataMapper.selectOutlier("eleven", resultMin.getEleven(), resultMax.getEleven());
        int twelve = analysisDataMapper.selectOutlier("twelve", resultMin.getTwelve(), resultMax.getTwelve());
        map.put("one", one);
        map.put("two", two);
        map.put("three", three);
        map.put("five", five);
        map.put("four", four);
        map.put("six", six);
        map.put("seven", seven);
        map.put("eight", eight);
        map.put("nine", nine);
        map.put("ten", ten);
        map.put("twelve", twelve);
        map.put("eleven", eleven);
        return map;
    }

    @Override
    public Integer factor() {
        return analysisDataMapper.getFactor();
    }

    @Override
    public Page<ResultVO> getData(Integer pageNum, Integer pageSize) {
        Page<ResultVO> resultVOPage = new Page<>();
        long total = analysisDataMapper.getTotalNum();
        resultVOPage.setResultCount(total);
        resultVOPage.setCurrentPage(pageNum);
        resultVOPage.setPageSize(pageSize);
        resultVOPage.getPageCount();
        resultVOPage.setData(analysisDataMapper.getData(resultVOPage.getStart(), resultVOPage.getPageSize()));
        return resultVOPage;
    }
}
